<template>
    <div>
        <el-form label-width="120px" :model="Billing" ref="AUFinanceInvoiceref">
            <el-row>
                <el-col :offset="2" :span="20">
                    <el-form-item label="领票人">
                        <el-select width="100%" filterable v-model="InvoiceReceiver" filterable placeholder="请选择领票人" style="width:100%">
                            <el-option  value="">全部</el-option>
                            <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <div v-for="(Billing,i) in BillingArray" :key="i">
                <el-row>
                    <el-col :offset="2" :span="9">
                        <el-form-item label="发票代码">
                            <el-input type="text" v-model="Billing.InvoiceCode"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :offset="2" :span="9">
                        <el-form-item label="发票号码">
                            <el-input type="text" v-model="Billing.InvoiceNumber"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :offset="2" :span="18">
                        <el-form-item label="金额">
                            <el-input type="text" v-model="Billing.Money"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2" style="text-align:right">
                        <el-button v-if="i==0" icon="el-icon-circle-plus" circle @click="AddRow"></el-button>
                        <el-button v-else icon="el-icon-delete" circle @click="DelRow(i)"></el-button>
                    </el-col>
                </el-row>
            </div>
            <el-row>
                <el-col style="text-align: center">
                    <el-button type="primary" @click="SaveFinance">提交</el-button>
                </el-col>
            </el-row>

        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'AddFinanceInvoiceManage',
        props: {
            AllUsers: {
                type: Array,
                required: true
            },
            BillNumber: {
                type: String,
                required: true
            },
        },
        data() {
            return {
                InvoiceReceiver: '',
                BillingArray: [],
                Billing: {
                    Id: 0,
                    InvoiceCode: '',
                    InvoiceNumber: '',
                    Money: '',
                },
            }
        },

        created() {
            this.BillingArray.push(JSON.parse(JSON.stringify(this.Billing)));
        },
        methods: {
            AddRow() {
                this.BillingArray.push(JSON.parse(JSON.stringify(this.Billing)));
            },
            DelRow(obj) {
                this.BillingArray.splice(obj, 1);
            },
            SaveFinance() {
                var result = true;
                var thisBillingArray = this.BillingArray;
                var financeInvoiceManages = [];
                if (thisBillingArray[0].InvoiceNumber.length > 0) {
                    for (var i = 0; i < thisBillingArray.length; i++) {
                        var count = financeInvoiceManages.find(x => x.InvoiceNumber == thisBillingArray[i].InvoiceNumber);
                        if (count != null || count != undefined) {
                            result = false;
                            this.$message.error("存在重复的发票号码");
                            return false;
                        }
                        financeInvoiceManages.push(thisBillingArray[i]);
                    }
                } else {
                    this.$message.error("请填写相关数据");
                    return false;
                }
                if (!result)
                    return false;
                let loadding = this.openLoading();
                this.$http.post("/Finance/AddFinanceInvoiceManage", { financeInvoiceManages: thisBillingArray, BillNumber: this.BillNumber, InvoiceReceiver: this.InvoiceReceiver })
                    .then((list) => {
                        loadding.close();
                        let result = list.data;
                        if (result.IsSuccess) {
                            this.$message.success(result.Msg);
                            this.$parent.$parent.AddFinanceInvoiceManageShow = false;
                            this.BillingArray = [];
                            this.InvoiceReceiver = ''
                            this.BillingArray.push(JSON.parse(JSON.stringify(this.Billing)));
                        }
                        else {
                            this.$message.error(result.Msg);
                        }
                    });
            },
        },
    }
</script>
<style scoped>
</style>